﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for NhapGiayDep.xaml
    /// </summary>
    public partial class NhapGiayDep : Page
    {
        private string m_sMaNhaPP;
        private string m_sMaLoaiSP;
        private SelectDB selectDB;
        private UpdateDB updateDB;
        private InsertDB insertDB;
        private SetID setId;
        private OtherHandle otherHandle;
        private int TongTien;
        private string MaNhomSP;
        private string MaPhieuNhapHang;
        private bool m_bSelectData = false;
        string URL;
       
        
        public NhapGiayDep()
        {
            InitializeComponent();
            dtmNgayNhap_NSP.Text = DateTime.Today.ToString();       
        }

        public NhapGiayDep(string _URL)
        {
            InitializeComponent();
            URL = _URL;
            otherHandle = new OtherHandle();
            updateDB = (UpdateDB)Activator.GetObject(typeof(UpdateDB), URL + "UPDATEDB_OBJECT");
            insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");
            selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
            setId = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");
            dtmNgayNhap_NSP.Text = DateTime.Today.ToString();
            ShowComboboxNhaPP(cmbTenNhaPP_NSP, 1);
            ShowComboboxLoaiSP(cmbTheLoai_NS, 1);
        }

        void ShowComboboxNhaPP(ComboBox cmb, int TinhTrang)
        {
            for (int i = 0; i < selectDB.SelectAllNhaPhanPhoi(TinhTrang).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllNhaPhanPhoi(TinhTrang)[i].MaNhaPP.ToString());
            }
        }

        void ShowComboboxLoaiSP(ComboBox cmb, int TinhTrang)
        {
            for (int i = 0; i < selectDB.SelectAllLoaiSanPham(TinhTrang).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllLoaiSanPham(TinhTrang)[i].MaLoaiSanPham.ToString());
            }
        }

        void ShowComboboxDauSP(ComboBox cmb, string m_sMaNhaPP, string m_sMaLoaiSP)
        {
            for (int i = 0; i < selectDB.SelectAllDauSanPham(m_sMaLoaiSP, m_sMaNhaPP).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllDauSanPham(m_sMaLoaiSP, m_sMaNhaPP)[i].ToString());
            }
        }

        //them mot nha phan phoi moi
        private void btnThemNhaPP_NSP_Click(object sender, RoutedEventArgs e)
        {
            WinThemNhaPP m_wThemNhaPP = new WinThemNhaPP(URL);
            m_wThemNhaPP.Show();
        }

        //hien thi danh sach cac nha phan phoi vua nhap trong combobox
        private void btnRefeshNhaPP_NSP_Click(object sender, RoutedEventArgs e)
        {
            cmbTenNhaPP_NSP.Items.Clear();
            ShowComboboxNhaPP(cmbTenNhaPP_NSP, 1);
        }

        // them mot ma loai sp moi
        private void btnThemLoaiSP_NSP_Click(object sender, RoutedEventArgs e)
        {
            WinThemLoaiSP m_wThemLoaiSP = new WinThemLoaiSP(URL);
            m_wThemLoaiSP.Show();
        }

        private void btnRefeshLoaiSP_NSP_Click(object sender, RoutedEventArgs e)
        {
            cmbTheLoai_NS.Items.Clear();
            ShowComboboxLoaiSP(cmbTheLoai_NS, 1);
        }

        //Them mot dau san pham
        private void btnThemDauSP_NSP_Click(object sender, RoutedEventArgs e)
        {
            m_sMaNhaPP = cmbTenNhaPP_NSP.SelectedItem.ToString();
            m_sMaLoaiSP = cmbTheLoai_NS.SelectedItem.ToString();
            WinThemDauSP m_wThemDauSP = new WinThemDauSP(m_sMaNhaPP, m_sMaLoaiSP, URL);
            m_wThemDauSP.Show();
        }

        private void btnDongY_Click(object sender, RoutedEventArgs e)
        {
            cmbTenSP_NSP.IsEnabled = true;
            txtGiaBan_NSP.IsEnabled = true;
            txtThoiHanBan_NS.IsEnabled = true;
            btnThemDauSP_NSP.IsEnabled = true;
            btnSuaDauSP_NSP.IsEnabled = true;
            btnRefeshDauSP.IsEnabled = true;
            btnOkDauSP_NSP.IsEnabled = true;
            ShowComboboxDauSP(cmbTenSP_NSP, cmbTenNhaPP_NSP.SelectedItem.ToString(), cmbTheLoai_NS.SelectedItem.ToString());
        }

        private void cmbTenSP_NSP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                //selectDB=new SelectDB();
               
                txtThoiHanBan_NS.Text = selectDB.SelectAllDauSanPham(cmbTenSP_NSP.SelectedItem.ToString()).ThoiHanBan.ToString();
                txtGiaBan_NSP.Text = selectDB.SelectAllDauSanPham(cmbTenSP_NSP.SelectedItem.ToString()).GiaBan.ToString();
            }
            else
            {
                m_bSelectData = false;
              
            }
        }

        private void btnSuaDauSP_NSP_Click(object sender, RoutedEventArgs e)
        {
            //updateDB = new UpdateDB();
            m_bSelectData = true;
            updateDB.UpdateDauSanPham(cmbTenSP_NSP.SelectedItem.ToString(), Int32.Parse(txtGiaBan_NSP.Text),
                                      Int32.Parse(txtThoiHanBan_NS.Text));
        }

        private void btnRefeshDauSP_Click(object sender, RoutedEventArgs e)
        {
            cmbTenSP_NSP.Items.Clear();
            
            ShowComboboxDauSP(cmbTenSP_NSP, cmbTenNhaPP_NSP.SelectedItem.ToString(), cmbTheLoai_NS.SelectedItem.ToString());
           
        }

        private void btnOkDauSP_NSP_Click(object sender, RoutedEventArgs e)
        {
            cmbTenSP_NSP.IsEnabled = false;
            txtGiaBan_NSP.IsEnabled = false;
            txtThoiHanBan_NS.IsEnabled = false;
            btnThemDauSP_NSP.IsEnabled = false;
            btnSuaDauSP_NSP.IsEnabled = false;
            btnRefeshDauSP.IsEnabled = false;
            btnOkDauSP_NSP.IsEnabled = false;
            btnDongY.IsEnabled = false;
            btnChapNhanNhap.IsEnabled = true;


        }

        private void btnThemCTNhap_NSP_Click(object sender, RoutedEventArgs e)
        {
            //insertDB = new InsertDB();
            //setId = new SetID();
            MaNhomSP = setId.GenerateMaNhomSP();
            MaPhieuNhapHang = setId.GenerateMaPhieuNhap();
            int kt;
            
            kt = insertDB.InsertNhomSanPham(MaNhomSP, cmbTenSP_NSP.SelectedItem.ToString(), txtMauSac_NSP.Text,
                                            float.Parse(txtKichThuoc_NSP.Text), Int32.Parse(txtSoLuong_NSP.Text));
            DateTime m_dtNgayTra = dtmNgayNhap_NSP.SelectedDate.Value;
            kt = insertDB.InsertPhieuNhapHang(MaPhieuNhapHang, m_dtNgayTra.ToString("yyyy/MM/dd"), TongTien);
            kt = insertDB.InsertCTPhieuNhap(MaPhieuNhapHang, MaNhomSP, Int32.Parse(txtSoLuong_NSP.Text),
                                            Int32.Parse(txtGiaVon_NSP.Text));
            for (int i = 0; i < Int32.Parse(txtSoLuong_NSP.Text); i++ )
            {
                SetID m_setIDTemp = (SetID)Activator.GetObject(typeof(SetID), URL + "SETID_OBJECT");
                string m_sMaSPTemp = m_setIDTemp.GenerateMaSanPham();
                kt = insertDB.InsertSanPham(m_sMaSPTemp, MaNhomSP, MaPhieuNhapHang, 1);
            }

                if (kt == -1)
                    MessageBox.Show("Thêm sản phẩm thất bại", "Lỗi");
                else
                {
                    MessageBox.Show("Thêm sản phẩm Thành Công", "Thông Báo");
                }
        }

        private void bntTiepTucNhap_NSP_Click(object sender, RoutedEventArgs e)
        {
            btnThemCTNhap_NSP.IsEnabled = false;
            btnDongY.IsEnabled = true;
            MaNhomSP = "";
            MaPhieuNhapHang = "";
            txtTongTien_NSP.Text = "";
            TongTien = 0;
        }

        private void btnChapNhanNhap_Click(object sender, RoutedEventArgs e)
        {
            btnThemCTNhap_NSP.IsEnabled = true;
            TongTien = Int32.Parse(txtGiaVon_NSP.Text) * Int32.Parse(txtSoLuong_NSP.Text);
            txtTongTien_NSP.Text = TongTien.ToString();
            bntTiepTucNhap_NSP.IsEnabled = true;
        }

        private void txtKichThuoc_NSP_TextChanged(object sender, TextChangedEventArgs e)
        {
            //otherHandle = (OtherHandle)Activator.GetObject(typeof(OtherHandle), URL + "OTHERHANDLE_OBJECT");
            otherHandle = new OtherHandle();
            otherHandle.IsNumber(txtKichThuoc_NSP);
        }

        private void txtSoLuong_NSP_TextChanged(object sender, TextChangedEventArgs e)
        {
            //otherHandle = (OtherHandle)Activator.GetObject(typeof(OtherHandle), URL + "OTHERHANDLE_OBJECT");
            otherHandle = new OtherHandle();
            otherHandle.IsNumber(txtSoLuong_NSP);
        }

        private void txtGiaVon_NSP_TextChanged(object sender, TextChangedEventArgs e)
        {
            //otherHandle = (OtherHandle)Activator.GetObject(typeof(OtherHandle), URL + "OTHERHANDLE_OBJECT");
            otherHandle = new OtherHandle();
            otherHandle.IsNumber(txtGiaVon_NSP);
        }

        private void txtGiaBan_NSP_TextChanged(object sender, TextChangedEventArgs e)
        {
            //otherHandle = (OtherHandle)Activator.GetObject(typeof(OtherHandle), URL + "OTHERHANDLE_OBJECT");
            otherHandle = new OtherHandle();
            otherHandle.IsNumber(txtGiaBan_NSP);
        }

        private void txtThoiHanBan_NS_TextChanged(object sender, TextChangedEventArgs e)
        {
            //otherHandle = (OtherHandle)Activator.GetObject(typeof(OtherHandle), URL + "OTHERHANDLE_OBJECT");
            otherHandle = new OtherHandle();
            otherHandle.IsNumber(txtThoiHanBan_NS);
        }

        
    }
}
